﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using PurchaseOrderExtra;
using System.Transactions;

namespace BusinessLogicLayer
{
    public class RaisePurchaseOrderControl
    {
        SupplierRepository supplierRepo;
        UserRepository userRepo;
        EmployeeRepository employeeRepo;
        TempPOItemRepository tempPOItemRepo;
        SupplierDetailRepository supplierDetailRepo;
        PurchaseOrderRepository purchaseOrderRepo;
        PurchaseOrderDetailRepository purchaseOrderDetailRepo;

        public RaisePurchaseOrderControl()
        {
            supplierRepo = new SupplierRepository();
            userRepo = new UserRepository();
            employeeRepo = new EmployeeRepository();
            tempPOItemRepo = new TempPOItemRepository();
            supplierDetailRepo = new SupplierDetailRepository();
            purchaseOrderRepo = new PurchaseOrderRepository();
            purchaseOrderDetailRepo = new PurchaseOrderDetailRepository();
        }

        public List<Supplier> DisplayAllSupplier()
        {
            List<Supplier> list = new List<Supplier>();
            Supplier supplier = new Supplier();
            supplier.SupplierID = "ALL";
            supplier.SupplierName = "All Suppliers";
            list.Add(supplier);
            List<Supplier> supList = supplierRepo.GetAllSupplier();
            foreach (Supplier sup in supList)
            {
                list.Add(sup);
            }
            return list;
        }

        public List<Employee> DisplayAllStoreClerk()
        {
            return employeeRepo.GetEmployeeByRoleName("Store Clerk");
        }

        public User GetUserByUsername(string username)
        {
            return userRepo.GetUserByUsername(username);
        }

        public Employee GetEmployeeByUserId(int userId)
        {
            return employeeRepo.GetEmployeeByUserId(userId);
        }

        public List<TempPurchaseOrderDisplay> GetTempPOItemByClerkIdAndSupplierId(User user, string supplierId)
        {
            int clerkId = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;

            List<TempPOItem> tempList = new List<TempPOItem>();
            tempList = tempPOItemRepo.GetTempPOItemByClerkIdAndSupplierId(clerkId, supplierId);

            List<TempPurchaseOrderDisplay> list = new List<TempPurchaseOrderDisplay>();
            foreach (TempPOItem temp in tempList)
            {
                int quantity = (int) temp.Quantity;
                decimal unitPrice = (decimal) supplierDetailRepo.GetSupplierDetailBySupplierIdAndItemCode(temp.SupplierID, temp.ItemCode).Price;
                TempPurchaseOrderDisplay poDisplay = new TempPurchaseOrderDisplay();
                poDisplay.TempPOId = temp.TempPOID;
                poDisplay.ItemCode = temp.ItemCode;
                poDisplay.ItemName = temp.Stationery.ItemName;
                poDisplay.Quantity = quantity;
                poDisplay.UnitOfMeasurement = temp.Stationery.UOM;
                poDisplay.UnitPrice = unitPrice;
                poDisplay.Amount = CalculateAmount(quantity, unitPrice);
                list.Add(poDisplay);
            }
            return list;
        }

        public void UpdateTempPOItem(int tempPOId, int quantity)
        {
            try
            {
                TempPOItem tempPOItem = tempPOItemRepo.GetTempPOItemByTempPOId(tempPOId);
                tempPOItem.Quantity = quantity;
                tempPOItemRepo.UpdateTempPOItem(tempPOItem);
            }
            catch (Exception ex)
            {
            }
        }

        public void DeleteTempPOItem(int tempPOId)
        {
            try
            {
                tempPOItemRepo.DeleteTempPOItem(tempPOId);
            }
            catch (Exception ex)
            {
            }
        }

        public void DeleteTempPOAndCreatePurchaseOrder(User user, int attnId, DateTime expectedDate, List<int> tempPOIdList)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    // Add to PurchaseOrder table
                    PurchaseOrder purchaseOrder = new PurchaseOrder();  
                    purchaseOrder.RequestedDate = DateTime.Now;
                    purchaseOrder.ExpectedDeliveryDate = expectedDate;
                    purchaseOrder.SupplierID = tempPOItemRepo.GetTempPOItemByTempPOId(tempPOIdList[0]).SupplierID;
                    purchaseOrder.Attn = attnId;
                    purchaseOrder.RequestedBy = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;
                    purchaseOrder.Status = "Pending";
                    purchaseOrderRepo.CreatePurchaseOrder(purchaseOrder);

                    for (int i = 0; i < tempPOIdList.Count; i++)
                    {
                        // Add to PurchaseOrderDetail table
                        TempPOItem tempPOItem = tempPOItemRepo.GetTempPOItemByTempPOId(tempPOIdList[i]);
                        PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                        purchaseOrderDetail.POID = purchaseOrder.POID;
                        purchaseOrderDetail.ItemCode = tempPOItem.ItemCode;
                        purchaseOrderDetail.POQty = tempPOItem.Quantity;
                        purchaseOrderDetailRepo.CreatePurchaseOrderDetail(purchaseOrderDetail);

                        // Delete from TempPOItem table
                        tempPOItemRepo.DeleteTempPOItem(tempPOIdList[i]);
                    }

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
            }
        }

        public string RedirectToSelectMoreItemsForPurchasing()
        {
            string redirectURL = "/SelectItemsForPurchasing.aspx";
            return redirectURL; 
        }

        private decimal CalculateAmount(int quantity, decimal unitPrice)
        {
            return (quantity * unitPrice);
        }

        public List<RaisePODisplay> GetRaisePODisplay(User user)
        {
            int clerkId = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;

            List<RaisePODisplay> list = new List<RaisePODisplay>();

            List<Supplier> supList = supplierRepo.GetAllSupplier();
            foreach (Supplier sup in supList)
            {
                List<TempPOItem> tempPOItemList = tempPOItemRepo.GetTempPOItemByClerkIdAndSupplierId(clerkId, sup.SupplierID);
                int quantity = 0;
                foreach (TempPOItem tempPOItem in tempPOItemList)
                {
                    quantity = quantity + (int)tempPOItem.Quantity;
                }
                if (quantity > 0)
                {
                    RaisePODisplay raisePODisplay = new RaisePODisplay();
                    raisePODisplay.SupplierId = sup.SupplierID;
                    raisePODisplay.SupplierName = sup.SupplierName;
                    raisePODisplay.Quantity = quantity;
                    list.Add(raisePODisplay);
                }
            }
            return list;
        }

        public RaisePODisplay GetRaisePODisplayByUserAndSupplierId(User user, string supplierId)
        {
            int clerkId = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;
            List<TempPOItem> tempPOItemList = tempPOItemRepo.GetTempPOItemByClerkIdAndSupplierId(clerkId, supplierId);
            int quantity = 0;
            foreach (TempPOItem tempPOItem in tempPOItemList)
            {
                quantity = quantity + (int)tempPOItem.Quantity;
            }
            RaisePODisplay raisePODisplay = new RaisePODisplay();
            if (quantity > 0)
            {
                raisePODisplay.SupplierId = supplierId;
                raisePODisplay.SupplierName = tempPOItemList[0].Supplier.SupplierName;
                raisePODisplay.Quantity = quantity;
            }
            return raisePODisplay;
        }

        public string ViewRaisePurchaseOrder()
        {
            string redirectURL = "/RaisePurchaseOrder.aspx";
            return redirectURL;
        }

        public string RedirectToRaisePurchaseOrderList()
        {
            string redirectURL = "/RaisePurchaseOrderList.aspx";
            return redirectURL;
        }

    }
}
